package com.drinks.yiniang.ui.widget.nav;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.drinks.yiniang.R;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.utils.AppUtils;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 首页navigation bar
 * Created by han on 2018/10/17 0017.
 */

public class NavigationSearchBarLayout extends LinearLayout implements View.OnClickListener {

    private Context mContent;

    private AppCompatImageView mBackImageView;
    private AppCompatEditText mSearchEditView;
    private AppCompatTextView mEditTextView;

    private int mBackResId;
    private int mSearchResId;
    private int mCartResId;

    private String mSearchHintText;
    private String mEditText;

    private int mSearchTextSize;
    private int mSearchTextColor;
    private int mEditTextSize;
    private int mEditTextColor;

    //editTextView draw 位置, 默认drawleft
    private Drawable mEditDrawLeft;
    private Drawable mEditDrawTop;
    private Drawable mEditDrawRight;
    private Drawable mEditDrawBottom;


    private OnNavgationBarClickListener onNavgationBarClickListener;

    public NavigationSearchBarLayout(Context context) {
        this(context, null);
    }

    public NavigationSearchBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationSearchBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initTest();

        initData();
    }

    private void initView(Context content, AttributeSet attrs) {
        mContent = content;

        setOrientation(HORIZONTAL);

        TypedArray typedArray = mContent.obtainStyledAttributes(attrs, R.styleable.NavigationBarLayoutStyleable);

        mBackResId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarBackResId, R.mipmap.icon_back);
        mSearchResId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarSearchResId, R.mipmap.icon_nav_search_small);

        mSearchHintText = typedArray.getString(R.styleable.NavigationBarLayoutStyleable_navBarTitleText);
        mSearchTextSize = typedArray.getDimensionPixelSize(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextSize, 14);
        mSearchTextColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextColor, ContextCompat.getColor(content, R.color.color_main_black));

        mEditText = typedArray.getString(com.hansen.library.R.styleable.NavigationBarLayoutStyleable_navBarEditText);
        mEditTextSize = typedArray.getInt(com.hansen.library.R.styleable.NavigationBarLayoutStyleable_navBarEditTextSize, 14);
        mEditTextColor = typedArray.getColor(com.hansen.library.R.styleable.NavigationBarLayoutStyleable_navBarEditTextColor, ColorUtils.getColorById(mContent, R.color.color_main_black));

        mEditDrawLeft = typedArray.getDrawable(com.hansen.library.R.styleable.NavigationBarLayoutStyleable_android_drawableLeft);
        mEditDrawTop = typedArray.getDrawable(com.hansen.library.R.styleable.NavigationBarLayoutStyleable_android_drawableTop);
        mEditDrawRight = typedArray.getDrawable(com.hansen.library.R.styleable.NavigationBarLayoutStyleable_android_drawableRight);
        mEditDrawBottom = typedArray.getDrawable(com.hansen.library.R.styleable.NavigationBarLayoutStyleable_android_drawableBottom);

        typedArray.recycle();
    }

    private void initData() {
        int dp15 = ScreenSizeUtils.dp2px(mContent, 15);
        int dp10 = ScreenSizeUtils.dp2px(mContent, 10);
        int searchHeight = mContent.getResources().getDimensionPixelOffset(R.dimen.height_home_search);
        //获取顶部navbar高度
        int navBarHeight = mContent.getResources().getDimensionPixelSize(R.dimen.height_navigation_bar);

        //沉浸，设置状态栏高度, 加上&& !isInEditMode() 判断是因为在EditMode模式下，会出现exception
        if (AppUtils.isVersionCodeGreaterThanOrEqualToKITKAT() && !isInEditMode()) {
            setPadding(0, (int) ScreenSizeUtils.getStatusBarHeightWithAndroid(mContent), 0, 0);
        }

        mBackImageView = new AppCompatImageView(mContent);
        mSearchEditView = new AppCompatEditText(mContent);
        mEditTextView = new AppCompatTextView(mContent);

        //菜单
        mBackImageView.setId(R.id.iv_nav_bar_back);
        mBackImageView.setPadding(dp15, 0, dp15, 0);
        mBackImageView.setImageResource(mBackResId);
        mBackImageView.setOnClickListener(this);

        //搜索
        mSearchEditView.setId(R.id.iv_nav_bar_search);
        mSearchEditView.setPadding(dp10, 0, dp10, 0);
        mSearchEditView.setTextColor(mSearchTextColor);
        mSearchEditView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mSearchTextSize);
        mSearchEditView.setHint(mSearchHintText);
        mSearchEditView.setHintTextColor(ColorUtils.getColorById(mContent, R.color.color_b4b4b4));
        mSearchEditView.setLines(1);
        mSearchEditView.setInputType(InputType.TYPE_CLASS_TEXT);
        mSearchEditView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mSearchEditView.setCompoundDrawablePadding(dp10);
        mSearchEditView.setCompoundDrawablesWithIntrinsicBounds(mSearchResId, 0, 0, 0);
        mSearchEditView.setBackgroundResource(R.drawable.shape_bkg_search);

        //购物车
        mEditTextView.setId(R.id.tv_nav_bar_edit);
        mEditTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mEditTextSize);
        mEditTextView.setTextColor(mEditTextColor);
        mEditTextView.setLines(1);
        mEditTextView.setText(mEditText);
        mEditTextView.setGravity(Gravity.CENTER);
        mEditTextView.setPadding(dp10, 0, dp15, 0);
        mEditTextView.setCompoundDrawablesWithIntrinsicBounds(mEditDrawLeft, mEditDrawTop, mEditDrawRight, mEditDrawBottom);
        mEditTextView.setCompoundDrawablePadding(3);
        mEditTextView.setOnClickListener(this);

        LayoutParams paramsWrap = new LayoutParams(LayoutParams.WRAP_CONTENT, navBarHeight);
        LayoutParams paramsEdit = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        LayoutParams paramsSearch = new LayoutParams(0, searchHeight);

        paramsWrap.gravity = Gravity.CENTER_VERTICAL;

        paramsEdit.gravity = Gravity.CENTER_VERTICAL;

        paramsSearch.weight = 1.0f;
        paramsSearch.gravity = Gravity.CENTER_VERTICAL;

        addView(mBackImageView, paramsWrap);
        addView(mSearchEditView, paramsSearch);
        addView(mEditTextView, mEditDrawTop == null && mEditDrawBottom == null ? paramsWrap : paramsEdit);
    }

    private void initTest() {
        if (isInEditMode()) {
            mSearchHintText = "请输入搜索内容";
        }
    }

    @Override
    public void onClick(View v) {
        if (onNavgationBarClickListener == null) {
            return;
        }

        switch (v.getId()) {
            case R.id.iv_nav_bar_back: //返回
                onNavgationBarClickListener.onBackClick(v);
                break;
            case R.id.tv_nav_bar_edit: //购物车
                onNavgationBarClickListener.onEditClick(v);
                break;
            default:
                break;
        }
    }

    public EditText getNavBarSearchEditText() {
        return mSearchEditView;
    }

    //设置搜索text
    public void setNavBarSearchText(@StringRes int resId) {
        setNavBarSearchText(getContext().getString(resId));
    }

    public String getNavBarSearchText() {
        if (mSearchEditView != null) {
            return StringUtils.getEditTextString(mSearchEditView.getText());
        }

        return null;
    }

    //设置搜索text
    public void setNavBarSearchText(String text) {
        if (mSearchEditView != null) {
            mSearchEditView.setText(text);
        }
    }

    public void setOnNavgationBarClickListener(OnNavgationBarClickListener onNavgationBarClickListener) {
        this.onNavgationBarClickListener = onNavgationBarClickListener;
    }
}
